Partial Class Admin_Add_Contact_Broker
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Name_txt.Text = "" Then
      LoadDetails()
      If Session("contact_mode") = "View" Then
        DisableMost()
      End If

    End If
        SecurePage()
  End Sub

  Private Sub DisableMost()
    LastName_txt.Enabled = False : FirstName_txt.Enabled = False : MiddleName_txt.Enabled = False
    F1_txt.Enabled = False : T1_txt.Enabled = False : M1_txt.Enabled = False : O1_txt.Enabled = False
    Position_txt.Enabled = False : Email_txt.Enabled = False : Save_btn.Enabled = False
  End Sub

  Private Sub LoadDetails()
    Dim sConn As New Data.SqlClient.SqlConnection
    Dim sCom As New Data.SqlClient.SqlCommand
    Dim sDApt As New Data.SqlClient.SqlDataAdapter(sCom)
    Dim sDT As New Data.DataTable
    Dim zClass As New LookUp.General_Lookup

    Try
      sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")
      sCom.CommandType = Data.CommandType.Text
      sCom.Connection = sConn

      If Session("contact_mode") = "Edit" Then
        sCom.CommandText = "SELECT broker_mstr.broker_desc, broker_contact_dtl.* " & _
          "FROM broker_contact_dtl, broker_mstr " & _
          "WHERE broker_contact_dtl.broker_cd = broker_mstr.broker_cd " & _
          "AND broker_contact_cd='" & Session("contact_cd") & "'"
        sConn.Open()
        sDApt.Fill(sDT)

        With sDT.Rows(0)
          Name_txt.Text = .Item(0).ToString
          LastName_txt.Text = .Item(3).ToString
          FirstName_txt.Text = .Item(4).ToString
          MiddleName_txt.Text = .Item(5).ToString
          Position_txt.Text = .Item(6).ToString
          T1_txt.Text = .Item(7).ToString
          O1_txt.Text = .Item(8).ToString
          F1_txt.Text = .Item(9).ToString
          M1_txt.Text = .Item(10).ToString
          Email_txt.Text = .Item(11).ToString
        End With

      Else

        sCom.CommandText = "SELECT broker_desc FROM broker_mstr WHERE broker_cd='" & Session("broker_cd") & "'"
        sConn.Open()
        sDApt.Fill(sDT)

        With sDT.Rows(0)
          Name_txt.Text = .Item(0).ToString
        End With

      End If

    Catch ex As Exception

    Finally
      sConn.Close()
    End Try

  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    Session.Remove("contact_cd") : Session.Remove("contact_mode")
    Response.Redirect("~/Admin/Add_Broker.aspx")
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
    Dim zClass As New LookUp.General_Lookup
    Dim dm As New DAOMain.CommonDAO
    Dim strTemp As String = ""

        If Trim(LastName_txt.Text) = "" Or Trim(FirstName_txt.Text) = "" Then
            Dim pFunc As New PolyFunc.ASPFunc

            pFunc.MessageAlert(Me, "First Name and Last Name are required!")
            pFunc = Nothing
            Exit Sub
        End If
        If Session("contact_mode") = "Edit" Then
            zClass.ExecQuery("UPDATE broker_contact_dtl " & _
              "SET broker_contact_cd='" & Trim(Session("contact_cd").ToString) & _
              "', broker_cd='" & Trim(Session("broker_cd").ToString) & _
              "', broker_dtl_last_name=" & UCase(dm.Q(Trim(LastName_txt.Text))) & _
              ", broker_dtl_first_name=" & UCase(dm.Q(Trim(FirstName_txt.Text))) & _
              ", broker_dtl_middle_name=" & UCase(dm.Q(Trim(MiddleName_txt.Text))) & _
              ", broker_dtl_position=" & UCase(dm.Q(Trim(Position_txt.Text))) & _
              ", tel_no=" & UCase(dm.Q(Trim(T1_txt.Text))) & _
              ", office_no=" & UCase(dm.Q(Trim(O1_txt.Text))) & _
              ", fax_no=" & UCase(dm.Q(Trim(F1_txt.Text))) & _
              ", mobile_no=" & UCase(dm.Q(Trim(M1_txt.Text))) & _
              ", email_address=" & UCase(dm.Q(Trim(Email_txt.Text))) & _
              ", broker_dtl_last_upd_by='" & UCase(Session("UserName")) & "'" & _
              ", broker_dtl_last_upd_dt='" & _
              Now & "'" & _
               " WHERE broker_contact_cd='" & Trim(Session("contact_cd").ToString) & "'")

        ElseIf Session("contact_mode") = "Add" Then

            zClass.LookUpCode = "BROKER_CONTACT"
            strTemp = zClass.MaxCode()
            zClass.ExecQuery("INSERT INTO broker_contact_dtl(broker_contact_cd,broker_cd, broker_dtl_last_name, " & _
                "broker_dtl_first_name, broker_dtl_middle_name, broker_dtl_position, tel_no, office_no, fax_no, " & _
                "mobile_no, email_address, broker_dtl_crea_by, broker_dtl_crea_dt) " & _
                "VALUES('" & strTemp & "','" & _
                Trim(Session("broker_cd").ToString) & "'" & _
                "," & UCase(dm.Q(Trim(LastName_txt.Text))) & _
                "," & UCase(dm.Q(Trim(FirstName_txt.Text))) & _
                "," & UCase(dm.Q(Trim(MiddleName_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Position_txt.Text))) & _
                "," & UCase(dm.Q(Trim(T1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(O1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(F1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(M1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Email_txt.Text))) & _
                ",'" & UCase(Session("UserName")) & "'" & _
                ",'" & Now & "')")

        End If

    Response.Redirect("~/Admin/Add_Broker.aspx")
  End Sub
End Class